package com.ikun.yummy.sourceop.op;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.ikun.yummy.bean.LikeInfo;
import com.ikun.yummy.properties.AppProperties;
import com.ikun.yummy.sourceop.sqlutils.SqliteHelper;

import java.util.ArrayList;
import java.util.List;
public class LikeOP {
    private final SqliteHelper helper;
    private final SQLiteDatabase db;
    public LikeOP(Context context){
        helper = new SqliteHelper(context);
        db = helper.getReadableDatabase();
    }
    public List<LikeInfo> insert(LikeInfo likeInfo){
        db.beginTransaction();
        ContentValues values = new ContentValues();
        values.put("username", likeInfo.username);
        values.put("cookname", likeInfo.cookname);
        values.put("cookid", likeInfo.likeid);
        db.insert(AppProperties.LIKE_TABLE, null, values);
        db.setTransactionSuccessful();
        db.endTransaction();
        return query(likeInfo.likeid,likeInfo.username);
    }
    public void delete(String likeid) {
        db.delete(AppProperties.LIKE_TABLE, "likeid=?",new String[]{likeid});
    }
    public List<LikeInfo> query(String cookid,String username){//选择性返回我点过的赞
      String sql="";
        if (cookid!=null&&username!=null){
            sql="SELECT * FROM "+AppProperties.LIKE_TABLE+" WHERE cookid="+cookid+" AND username='"+username+"'";
        }else if (cookid!=null){
            sql="SELECT * FROM "+AppProperties.LIKE_TABLE+" WHERE cookid="+cookid;
        }else{
            sql="SELECT * FROM "+AppProperties.LIKE_TABLE+" WHERE username='"+username+"'";
        }
        @SuppressLint("Recycle") Cursor cursor =db.rawQuery(sql,null,null);
        if (cursor!=null&&cursor.getCount()>0){
            List<LikeInfo> list=new ArrayList<>();
            while (cursor.moveToNext()){
                list.add(new LikeInfo(cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3)));
            }
            return list;
        }else{
            return null;
        }
    }
}